/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2015-07-29     Arda.Fu      first implementation
 */
#include <stdint.h>
#include <rtthread.h>
#include <rtdevice.h>

/* defined the LED1 pin: PB5 */
/* defined the LED2 pin: PB4 */
/* defined the LED3 pin: PA8 */
// #define LED1_PIN    57
#define LED1_PIN    91 // work fine
#define LED2_PIN    90
#define LED3_PIN    67
#define SPI1_CS     29
#define W5500_RST   WIZ_RST_PIN
// #define LED1_PIN    GET_PIN(B, 5)

#include "n32g45x_gpio.h"
void test_jtag(void)
{
    volatile uint32_t tmpregister = 0x00;

    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO, ENABLE);
    GPIO_ConfigPinRemap(GPIO_RMP_SW_JTAG_NO_NJTRST, ENABLE);
    AFIO->RMP_CFG = 0x02000000;
    tmpregister = AFIO->RMP_CFG;
    rt_kprintf("rmp register %x\r\n", tmpregister);
    rt_kprintf("rmp register %x\r\n", AFIO->RMP_CFG);

    rt_kprintf("rmp2 register %x\r\n", AFIO->RMP_CFG2);
    rt_kprintf("rmp3 register %x\r\n", AFIO->RMP_CFG3);
    rt_kprintf("rmp4 register %x\r\n", AFIO->RMP_CFG4);
    rt_kprintf("rmp5 register %x\r\n", AFIO->RMP_CFG5);
    GPIO_ConfigPinRemap(GPIO_RMP_SW_JTAG_DISABLE, DISABLE);
    tmpregister = AFIO->RMP_CFG;
    rt_kprintf("rmp register %x\r\n", tmpregister);
}
MSH_CMD_EXPORT(test_jtag, test);


int main(void)
{
    uint32_t pin = LED1_PIN;
    uint32_t Speed = 200;
    /* set LED1 pin mode to output */
    rt_pin_mode(pin, PIN_MODE_OUTPUT);

    while (1)
    {
        rt_pin_write(pin, PIN_LOW);
        rt_thread_mdelay(Speed);
        rt_pin_write(pin, PIN_HIGH);
        rt_thread_mdelay(Speed);
    }
}

void test_led(int argc, char *argv[])
{
    uint32_t pin = LED1_PIN;
    uint32_t pin_desc = 1;
    uint32_t Speed = 200;
    if(argc == 2)
    {
        if (rt_strncmp(argv[1], "1", 1) == 0)
        {
            pin = LED1_PIN;
            pin_desc = 1;
        }
        else if (rt_strncmp(argv[1], "2", 1) == 0)
        {
            RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO, ENABLE);
            GPIO_ConfigPinRemap(GPIO_RMP_SW_JTAG_NO_NJTRST, ENABLE);
            pin = LED2_PIN;
            pin_desc = 2;
        }
        else if (rt_strncmp(argv[1], "3", 1) == 0)
        {
            pin = LED3_PIN;
            pin_desc = 3;
        }
        else if (rt_strncmp(argv[1], "4", 1) == 0)
        {
            pin = SPI1_CS;
            pin_desc = 4;
        }
        else if (rt_strncmp(argv[1], "5", 1) == 0)
        {
            pin = W5500_RST;
            pin_desc = 5;
        }
    }

    rt_pin_mode(pin, PIN_MODE_OUTPUT);

    for (int i = 0; i < 50; i++)
    {
        rt_pin_write(pin, PIN_LOW);
        rt_kprintf("led%d low\r\n", pin_desc);
        rt_thread_mdelay(Speed);
        rt_pin_write(pin, PIN_HIGH);
        rt_kprintf("led%d high\r\n", pin_desc);
        rt_thread_mdelay(Speed);
    }
}
MSH_CMD_EXPORT(test_led, test);

static void pc5_isr(void)
{
    /* enter interrupt */
    // rt_interrupt_enter();

    rt_kprintf("5");

    /* leave interrupt */
    // rt_interrupt_leave();
}

void test_irq(void)
{
    /* initialize interrupt pin */
    rt_pin_detach_irq(WIZ_IRQ_PIN);
    rt_pin_mode(WIZ_IRQ_PIN, PIN_MODE_INPUT_PULLUP);
    rt_pin_attach_irq(WIZ_IRQ_PIN, PIN_IRQ_MODE_FALLING, (void (*)(void*)) pc5_isr, RT_NULL);
    rt_pin_irq_enable(WIZ_IRQ_PIN, PIN_IRQ_ENABLE);
}
MSH_CMD_EXPORT(test_irq, test);
